﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using DotSpatial.Controls;

namespace DotSpatial.SampleProjects.SampleProjectManager
{
    public class SampleProjectHelper
    {
        /// <summary>
        /// Finds all sample project files that are located in the installed extensions directory.
        /// </summary>
        /// <returns>The list of the sample project files</returns>
        public IEnumerable<string> FindSampleProjectFiles()
        {
            return Directory.EnumerateFiles(AppManager.AbsolutePathToExtensions, "*.dspx");
        }

        /// <summary>
        /// Given the project name (for example "test.dspx"), looks in the 
        /// lib and content folders to find the project full path
        /// </summary>
        /// <param name="projectName"></param>
        /// <returns></returns>
        public static string FindDefaultProjectFile(Assembly asm, string projectName)
        {
            string dir = Path.GetDirectoryName(asm.Location);
            string projFile = Path.Combine(dir, projectName);

            if (!File.Exists(projFile))
            {
                DirectoryInfo curDir = new DirectoryInfo(dir);

                //moving from [packageName/lib/net40-client] to [packageName/content]
                string greatGreatParentDir = curDir.Parent.Parent.FullName;
                string contentDir = Path.Combine(greatGreatParentDir, "content");
                projFile = Path.Combine(contentDir, projectName);
            }

            return projFile;
        }
    }
}
